﻿/* ==============================================================================
 * 功能描述：BattleModule  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/26 21:09:46
 * ==============================================================================*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ZZQFrameWork.AI;
using ZZQFrameWork;

/// <summary>
/// 怪物掉落类型
/// </summary>
public enum Enum_MonsterDropType
{
    None=0,
    Exp,//经验
    Gold,//金币
    Goods,//物品
    Equip//装备
}

public class MonsterDropData
{
    public Enum_MonsterDropType dropType;
    public int dropId;
    public int count;
    public float probability;

    public MonsterDropData(Enum_MonsterDropType type,int count,float probability,int id)
    {
        dropType = type;
        dropId = id;
        this.count = count;
        this.probability = probability;
    }
} 

[System.Serializable]
public class MonsterVO
{
    /// <summary>
    /// 系统分配id
    /// </summary>
    public int uid;
    public int id;
    public string name;
    public string prefabName;
    public string des;
    public int maxHp;
    public int hp;
    public int atk;
    public int def;
    public float atkSpeed;
    public List<MonsterDropData> dropDataList=new List<MonsterDropData>(); 

    public MonsterVO(int id)
    {
        MonsterConfigMonster monster = MonsterConfigProvider.Instance.GetMonsterById(id);
        if(monster==null)Debug.LogError("找不到该Id的怪物，id："+id);
        this.id = id;
        name = monster.name;
        prefabName = monster.prefabName;
        des = monster.des;
        maxHp = int.Parse(monster.hp);
        hp = maxHp;
        atk = int.Parse(monster.atk);
        def = int.Parse(monster.def);
        atkSpeed = float.Parse(monster.atkSpeed);
        //Debug.Log("怪物掉落数量："+monster.drop.Length);
        for (int i = 0; i < monster.drop.Length; i++)
        {
            MonsterConfigMonsterDrop drop = monster.drop[i];
            MonsterDropData data = new MonsterDropData((Enum_MonsterDropType)int.Parse(drop.type),int.Parse(drop.count),float.Parse(drop.probability),int.Parse(drop.id));
            dropDataList.Add(data);
        }
        ModuleManager.Instance.Get<BattleModule>().AddMonster(this);
    }

    public bool DeductHp(int value)
    {
        hp -= value;
        hp = hp < 0 ? 0 : hp;
        Debug.Log("被攻击，血量："+hp);
        EventDispatcher.TriggerEvent(Events.MonsterEvent.OnUpdateHp, uid, hp, maxHp);
        if (hp <= 0)
        {
            //TODO 角色死亡,发送死亡事件
            hp = 0;
            //ModuleManager.Instance.Get<BattleModule>().currentMonster = null;
            EventDispatcher.TriggerEvent(Events.MonsterEvent.OnMonsterDeath,uid);
            ModuleManager.Instance.Get<BattleModule>().RemoveMonster(this);
            Debug.Log("怪物死亡死亡！");
        }
        return true;
    }
}

public class BattleModule : BaseModule
{
    /// <summary>
    /// 所有怪物集合
    /// </summary>
    public List<MonsterVO> monsterList=new List<MonsterVO>(); 

    public MonsterAI currentMonster;
    public PlayerAI currentPlayer;

    //public bool isInAttack = false;

    public BattleModule()
    {
        AutoRegister = true;
        currentMonster = null;
        currentPlayer = null;
    }

    public void AddMonster(MonsterVO monster)
    {
        if(monsterList.Contains(monster))return;
        monsterList.Add(monster);
    }

    public void RemoveMonster(MonsterVO monster)
    {
        if (!monsterList.Contains(monster)) return;
        monsterList.Remove(monster);
    }

    public void StartBattle(PlayerAI player,MonsterAI monster)
    {
        currentPlayer = player;
        currentMonster = monster;
        UIManager.Instance.OpenUI(Enum_UIOpenType.OpenSelf, Enum_UIType.BattlePanel);
    }

    /// <summary>
    /// 开始战斗
    /// </summary>
    /// <param name="deathAI"></param>
    public void EndBattle(EntityParent deathAI)
    {
        currentPlayer.SelectNode = null;
        currentMonster.SelectNode = null;
        currentPlayer.StopBattleTimer();
        currentMonster.StopBattleTimer();
        UIManager.Instance.CloseUI(Enum_UIType.BattlePanel);
        if (deathAI is PlayerAI)
        {
            currentPlayer.blackBoard.AIState = AIState.DEATH_STATE;
            currentMonster.blackBoard.AIState = AIState.IDLE_STATE;
            UIManager.Instance.OpenUI(Enum_UIOpenType.OpenSelf, Enum_UIType.GameOverPanel);
        }
        else
        {
            Debug.Log("改变空闲状态");
            currentPlayer.blackBoard.AIState = AIState.IDLE_STATE;
            currentMonster.blackBoard.AIState = AIState.DEATH_STATE;
            PoolManager.Instance.UnSpawn(currentMonster.gameObject);
            currentMonster.nowNode.goods = null;
            currentMonster.nowNode.canWalk = true;
            currentMonster.nowNode.isMonster = false;
            //currentPlayer.Data.SetMonsterDrop(currentMonster.Data.dropDataList);
            currentPlayer.Data.OnKillMonster(currentMonster.Data);
        }
        Debug.Log(currentPlayer.blackBoard.AIState);
        currentPlayer = null;
        currentMonster = null;
    }

    /// <summary>
    /// 逃离战斗
    /// </summary>
    public void GetAwayBattle()
    {
        currentPlayer.SelectNode = null;
        currentMonster.SelectNode = null;
        currentPlayer.StopBattleTimer();
        currentMonster.StopBattleTimer();
        UIManager.Instance.CloseUI(Enum_UIType.BattlePanel);
        currentPlayer = null;
        currentMonster = null;
    }
}